<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Graduate School Application - Applicant Information</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link rel="stylesheet" type="text/css" href="../resource/columnCSS.css" /> 
	<link rel="stylesheet" type="text/css" href="../resource/styleCSS.css" /> 	
</head>
<body>
	<% 
		String citizen = request.getParameter("citizen");
		session.setAttribute("citizen", citizen);
	
		String fname = (String)session.getAttribute("fname");
		String lname = (String)session.getAttribute("lname");
		String mname = (String)session.getAttribute("mname");
		
		// Get Vector of countries
		
		/* JH: using support.java
 		support s = new support(); 
	 	String path = config.getServletContext().getRealPath("countries.txt");
		Vector countries = s.getCountries(path);*/
	%>	
	 <%-- Open Connection Code--%>
	<% 
		String db_url = "jdbc:postgresql://localhost:5432/CSE135_SP11";
		String user ="postgres";
		String passwd ="postgres";
		Connection conn;
		PreparedStatement pstmt;
		ResultSet rset;
		
		try {
			Class.forName("org.postgresql.Driver");	
		}
		catch (java.lang.ClassNotFoundException e){
			System.err.println("ClassNotFoundException: " + e.getMessage());
		}
					
		try {
			conn = DriverManager.getConnection(db_url, user, passwd);
	%>
	<%-- Lookup Code for Countries --%>
    <%
   	 	pstmt = conn.prepareStatement(
			"SELECT country_name " + 
			"FROM Countries"); 
    
		rset = pstmt.executeQuery();
		
		Vector<String> countries_1 = new Vector<String>();
		Vector<String> countries_2 = new Vector<String>();
		Vector<String> countries_3 = new Vector<String>();
		int count = 0; 
		
		while (rset.next()) {
			
			switch (count%3) {
				case 1:
					countries_1.add(rset.getString("country_name"));
					break;
				case 2:
					countries_2.add(rset.getString("country_name"));
					break;
				default:
					countries_3.add(rset.getString("country_name"));
					break;
			}
			
			count++;
		}
		
		//int tbl_size = countries.getRow();
    %>
	<p class="sansserif">
	<div class="container">
	<div class="center_div">
		<div class="colmask threecol">
		<div class="colmid">
		<div class="colleft">
			<div class="bodycol">
				<p>Applicant: <i><%= fname%> <%= mname%> <%= lname%></i></p> 
				<p>Citizenship: <i><%= citizen%></i></p>
				<br>	
				<p><b>Select Country of Residence</b></p>
				<% 
					String url = request.getRequestURI();
					int len = url.length();
					url = url.substring(0, len - 16);
					url = url.concat("addressJSP.jsp?residence=");
					
					out.print("<p> <a href=\"" + url + citizen + "\">");
					out.print("Same as citizenship</a></p>");
				%>
			</div>
			<div class="sidecol">
				<p><a href="../invalidate.jsp">Logout</a></p>
			</div>
		</div>
		</div>
		</div>
		<div style="clear:both;"></div>	
	
		<div class="colmask threecol">
			<div class="colmid">
			<div class="colleft">
			<div class="col1">
				<%
				String country;
						
				for (count = 0; count < countries_1.size(); count++) {
					
					country = countries_1.get(count);
					
					out.print("<p> <a href=\"" + url + country + "\">");
					out.print(country + "</a></p>");
				}
				%>
			</div>
			<div class="col2">
				<%
				for (count = 0; count < countries_2.size(); count++) {
					
					country = countries_2.get(count);
					
					out.print("<p> <a href=\"" + url + country + "\">");
					out.print(country + "</a></p>");
				}
				%>
			</div>
			<div class="col3">
				<%
				for (count = 0; count < countries_3.size(); count++) {
					
					country = countries_3.get(count);
					
					out.print("<p> <a href=\"" + url + country + "\">");
					out.print(country + "</a></p>");
				}
				%>
			</div>
			</div>
			</div>
		</div>
		<div style="clear:both;"></div>
		</div>
		</div>
	<%
		}
		catch(SQLException e){
			System.err.println("SQLException: " + e.getMessage());
		}
	%>	
</body>
</html>